package u.can.i.up;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Properties;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.gephi.statistics.spi.CRSMetrics;

import u.can.i.up.category.FactorUtils;
import u.can.i.up.db.mongo.AppAntivirusResult;
import u.can.i.up.db.mongo.AppAntivirusResultService;
import u.can.i.up.db.mysql.PackageNameDAO;
import u.can.i.up.db.postgresql.Aliases;
import u.can.i.up.db.postgresql.AliasesDAO;
import u.can.i.up.db.postgresql.TotalDownloads;
import u.can.i.up.db.postgresql.TotalDownloadsDAO;
import u.can.i.up.gexf.AliasesGenerator;

public class GEXFGenerator {

	static Logger logger = LogManager.getLogger();
	final static Properties props = System.getProperties();
	final static String separator = props.getProperty("file.separator");// .toLowerCase().startsWith("win"))?
	final static String userHome = props.getProperty("user.home");
	final static String userDir = props.getProperty("user.dir");
	final static String osName = props.getProperty("os.name").toLowerCase();
	final static String sysBit = props.getProperty("sun.arch.data.model");
	final static int scale_size = 200;

	final static String mTerm_Info = userHome + separator + ".crsmetrics" + separator + "term_info.txt";
	final static String mTermid_List = userHome + separator + ".crsmetrics" + separator + "termid_list.txt";
	final static String mCo_Matrix = userHome + separator + ".crsmetrics" + separator + "co_mat.txt";
	final static String mWaf_Matrix = userHome + separator + ".crsmetrics" + separator + "waf_mat.txt";
	final static String mAffinity_Matrix = userHome + separator + ".crsmetrics" + separator + "affinity_mat.txt";

	final static String mTermid_List_tmp = userHome + separator + ".crsmetrics" + separator + "termid_list_tmp.txt";
	final static String mCo_Matrix_tmp = userHome + separator + ".crsmetrics" + separator + "co_mat_tmp.txt";
	final static String mWaf_Matrix_tmp = userHome + separator + ".crsmetrics" + separator + "waf_mat_tmp.txt";

	final static String AliasesFilePath = userHome + separator + ".crsmetrics" + separator + "Aliases.gexf";
	final static String mAliases = userHome + separator + ".crsmetrics" + separator + "Aliases.txt";
	final static String mCRSPR = userHome + separator + ".crsmetrics" + separator + "CRSPR.txt";
	final static String mGScript = userDir + separator + "pyLibs" + separator + "GScript" + separator
			+ "path_construct.py";

	static {
		if (osName.startsWith("win")) {
			System.load(userDir + separator + "jniLibs" + separator + "libcrspg" + sysBit + ".dll");
		} else if (osName.startsWith("lin")) {
			System.load(userDir + separator + "jniLibs" + separator + "libcrspg" + sysBit + ".so");
		} else if (osName.startsWith("mac")) {
			System.load(userDir + separator + "jniLibs" + separator + "libcrspg" + sysBit + ".jnilib");
		}
	}

	public static native int testIntegerMethod(int num);

	public static native int calculateCRSPG(String mTermid_List, String mWaf_Matrix, String mCRSPR);

	public static void main(String argv[]) {
		int flag = 7;
		switch (flag) {
		case 1:
			// 先过滤，再用我的方法找所有路径.
			main1(argv);
			break;
		case 2:
			// 先过滤，再用景怡的方法找所有路径.
			main2(argv);
			break;
		case 3:
			// 直接生成简单路径, CRSPR时考虑了下载量和类别.
			main3(argv);
			break;
		case 4:
			// 直接生成简单路径，CRSPR时未考虑下载量和类别.
			main4(argv);
			break;
		case 5:
			// 根据exported筛选，实际上没什么用，因为ic3和relationApk在计算关联的时候，只有exported是true的才会关联起来.
			main_filtered_by_exported_with_no_crspr(argv);
			break;
		case 6:
			// 筛选百度应用AIG.
			main_baidu_filtered(argv);
			break;
		case 7:
			// 根据adslist筛选.
			main_filtered_by_exported_and_adslist(argv);
			break;
		default:
			break;
		}
	}

	// 根据exported筛选，实际上没什么用，因为ic3和relationApk在计算关联的时候，只有exported是true的才会关联起来.
	public static void main_filtered_by_exported_with_no_crspr(String argv[]) {
		Reader reader = null;
		try {
			// 1.Get aliases from the database.
			reader = Resources.getResourceAsReader("Configuration.xml");
			SqlSessionFactoryBuilder ssfBuilder = new SqlSessionFactoryBuilder();
			SqlSessionFactory sqlSessionFactory = ssfBuilder.build(reader);
			SqlSession sqlSession = sqlSessionFactory.openSession();
			AliasesDAO aliasesDAO = sqlSession.getMapper(AliasesDAO.class);
			List<Aliases> aliases = aliasesDAO.findAliasesFilteredByExported(0);

			// 2.Generate the Aliases.gexf
			AliasesGenerator.getInstant().addAliase(AliasesFilePath, aliases);
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				reader.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

	// 筛选百度应用AIG.
	public static void main_baidu_filtered(String argv[]) {
		Map<String, Integer> packageNameMap = new HashMap<String, Integer>();
		String fileName = "D:\\MyPHDProject\\4.5\\data\\baidu_packname_list.txt";
		String line = "";
		try {
			BufferedReader in = new BufferedReader(new FileReader(fileName));
			while ((line = in.readLine()) != null) {
				String packagename = line.split("\t")[0].trim();
				packageNameMap.put(packagename, 1);
			}
			in.close();
		} catch (IOException e) {
			e.printStackTrace();
		}

		Reader reader = null;
		try {
			// 1.Get aliases from the database.
			reader = Resources.getResourceAsReader("Configuration.xml");
			SqlSessionFactoryBuilder ssfBuilder = new SqlSessionFactoryBuilder();
			SqlSessionFactory sqlSessionFactory = ssfBuilder.build(reader);
			SqlSession sqlSession = sqlSessionFactory.openSession();
			AliasesDAO aliasesDAO = sqlSession.getMapper(AliasesDAO.class);
			List<Aliases> aliases = aliasesDAO.findAliases(0);
			List<Aliases> new_aliases = new ArrayList<Aliases>();
			for (Aliases a : aliases) {
				if (packageNameMap.get(a.getSapp()) != null || packageNameMap.get(a.getTapp()) != null) {
					new_aliases.add(a);
				}
			}
			// 2.Generate the Aliases.gexf
			AliasesGenerator.getInstant().addAliase(AliasesFilePath, new_aliases);
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				reader.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

	public static void main_generate_virus_gephi_filtered_by_Positives(int positive) {
		Reader reader = null;
		List<Aliases> aliases = null;
		// 1.Get aliases from the database.
		try {
			reader = Resources.getResourceAsReader("Configuration.xml");
			SqlSessionFactoryBuilder ssfBuilder = new SqlSessionFactoryBuilder();
			SqlSessionFactory sqlSessionFactory = ssfBuilder.build(reader, "postgresql");
			SqlSession sqlSession = sqlSessionFactory.openSession();
			AliasesDAO aliasesDAO = sqlSession.getMapper(AliasesDAO.class);
			aliases = aliasesDAO.findAliasesFilteredByExported(0);
		} catch (IOException e) {
			e.printStackTrace();
		}

		logger.info("Aliases size: " + aliases.size());

		// 2.Get packagename from the database.
		List<AppAntivirusResult> appAntivirusResultList = new AppAntivirusResultService()
				.getAppAntivirusResultsByPositives(positive);
		Map<String, Integer> packageNameMap = new HashMap<String, Integer>();
		try {
			reader = Resources.getResourceAsReader("Configuration.xml");
			SqlSessionFactoryBuilder ssfBuilder = new SqlSessionFactoryBuilder();
			SqlSessionFactory sqlSessionFactory = ssfBuilder.build(reader, "mysql");
			SqlSession sqlSession = sqlSessionFactory.openSession();
			PackageNameDAO packageNameDAO = sqlSession.getMapper(PackageNameDAO.class);
			if (appAntivirusResultList != null && !appAntivirusResultList.isEmpty()) {
				for (AppAntivirusResult aar : appAntivirusResultList) {
					String pn = packageNameDAO.getPackageName(aar.getMd5());
					if (packageNameMap.get(pn) == null) {
						packageNameMap.put(pn, 0);
					}
					int currentNum = packageNameMap.get(pn);
					packageNameMap.put(pn, (Integer.parseInt(aar.getPositives()) > currentNum)
							? (Integer.parseInt(aar.getPositives())) : (currentNum));
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

		Iterator<Map.Entry<String, Integer>> entries = packageNameMap.entrySet().iterator();
		while (entries.hasNext()) {
			// 3.generate the new aliases.
			Map.Entry<String, Integer> entry = entries.next();
			String pn = entry.getKey();
			List<Aliases> new_aliases = new ArrayList<Aliases>();
			for (Aliases a : aliases) {
				if (a.getSapp().equals(pn) || a.getTapp().equals(pn)) {
					new_aliases.add(a);
				}
			}
			if (new_aliases == null || new_aliases.isEmpty()) {
				continue;
			}
			// 4.Generate the Aliases.gexf
			String tmpFilePath = AliasesFilePath.substring(0, AliasesFilePath.lastIndexOf(separator));
			tmpFilePath += separator + "virus" + separator + pn + ".gexf";
			AliasesGenerator.getInstant().addAliase(tmpFilePath, new_aliases);
		}
	}

	// 先过滤，再用我的方法找所有路径.
	@SuppressWarnings("rawtypes")
	public static void main1(String argv[]) {
		Reader reader = null;
		try {
			// 1.Get aliases from the database.
			reader = Resources.getResourceAsReader("Configuration.xml");
			SqlSessionFactoryBuilder ssfBuilder = new SqlSessionFactoryBuilder();
			SqlSessionFactory sqlSessionFactory = ssfBuilder.build(reader);
			SqlSession sqlSession = sqlSessionFactory.openSession();
			AliasesDAO aliasesDAO = sqlSession.getMapper(AliasesDAO.class);
			List<Aliases> aliases = aliasesDAO.findAliases(0);
			TotalDownloadsDAO totalDownloadsDAO = sqlSession.getMapper(TotalDownloadsDAO.class);
			TotalDownloads totalDownloads = totalDownloadsDAO.findTotalDownloads(0);
			HashMap<Integer, Integer> downloadMap = new HashMap<Integer, Integer>();
			HashMap<Integer, Integer> categoryMap = new HashMap<Integer, Integer>();
			FactorUtils categoryFactorMatrix = FactorUtils.getInstance();

			// 2.1 Generate the term_info.txt
			CRSMetrics.getInstance().writeTerm_infor(aliases, mTerm_Info, downloadMap, categoryMap);
			// 2.2 Generate the termid_list.txt
			CRSMetrics.getInstance().writeBriefTermid_List(aliases, mTermid_List_tmp);
			// 2.3 Generate the co_mat.txt
			CRSMetrics.getInstance().calculateCoMatrix(mTerm_Info, mTermid_List_tmp, mCo_Matrix_tmp, "2");
			// 2.4 Generate the waf_mat.txt
			CRSMetrics.getInstance().calculateWafMatrix(mCo_Matrix_tmp, mTerm_Info, mWaf_Matrix_tmp, "1e-6");
			// 2.5 Calculate the WafValues
			Map<Integer, Float> returnValue = CRSMetrics.getInstance().calculateWafValues(mWaf_Matrix_tmp, downloadMap,
					totalDownloads, categoryMap, categoryFactorMatrix, scale_size, 5);
			// 2.6 Re-initial Aliases
			List<Aliases> newAliases = CRSMetrics.getInstance().ReinitialAliases(aliases, returnValue);
			new File(mTermid_List_tmp).delete();
			new File(mCo_Matrix_tmp).delete();
			new File(mWaf_Matrix_tmp).delete();

			// 3.1 Generate mAliases.
			// CSMetric.getInstance().writeAliases(newAliases, mAliases);
			// 3.2 Re-generate mTermid_List
			CRSMetrics.getInstance().writeTermid_List(newAliases, mTermid_List);
			// 按照收尾节点找路径代码
			// CSMetric.getInstance().writeTermid_List_WithStartAndEnd(newAliases,
			// mTermid_List);
			// 景怡同学的代码.
			// CSMetric.getInstance().writeGTermid_List(mGScript);
			// 3.3 Re-generate the co_mat.txt
			CRSMetrics.getInstance().calculateCoMatrix(mTerm_Info, mTermid_List, mCo_Matrix, "2");
			// 3.4 Re-generate the waf_mat.txt
			CRSMetrics.getInstance().calculateWafMatrix(mCo_Matrix, mTerm_Info, mWaf_Matrix, "1e-6");
			// 3.5 Re-calculate the WafValues
			Map<Integer, Float> returnValue2 = CRSMetrics.getInstance().calculateWafValues(mWaf_Matrix, downloadMap,
					totalDownloads, categoryMap, categoryFactorMatrix, scale_size, 5);
			// 3.6 Generate the affinity_mat.txt
			// CSMetric.getInstance().calculateAffinityMatrix(mWaf_Matrix,
			// mTerm_Info, mAffinity_Matrix, "0.04");
			// 3.6 Refresh returnValue2
			Iterator iter = returnValue2.entrySet().iterator();
			while (iter.hasNext()) {
				Map.Entry entry = (Map.Entry) iter.next();
				if ((float) entry.getValue() == -1) {
					returnValue2.put((int) entry.getKey(), 0.0f);
				}
			}
			// 4.Generate the Aliases.gexf
			AliasesGenerator.getInstant().addAliase(AliasesFilePath, aliases, returnValue2);
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				reader.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

	// 先过滤，再用景怡的方法找所有路径.
	@SuppressWarnings("rawtypes")
	public static void main2(String argv[]) {
		Reader reader = null;
		try {
			// 1.Get aliases from the database.
			reader = Resources.getResourceAsReader("Configuration.xml");
			SqlSessionFactoryBuilder ssfBuilder = new SqlSessionFactoryBuilder();
			SqlSessionFactory sqlSessionFactory = ssfBuilder.build(reader);
			SqlSession sqlSession = sqlSessionFactory.openSession();
			AliasesDAO aliasesDAO = sqlSession.getMapper(AliasesDAO.class);
			List<Aliases> aliases = aliasesDAO.findAliases(0);
			TotalDownloadsDAO totalDownloadsDAO = sqlSession.getMapper(TotalDownloadsDAO.class);
			TotalDownloads totalDownloads = totalDownloadsDAO.findTotalDownloads(0);
			HashMap<Integer, Integer> downloadMap = new HashMap<Integer, Integer>();
			HashMap<Integer, Integer> categoryMap = new HashMap<Integer, Integer>();
			FactorUtils categoryFactorMatrix = FactorUtils.getInstance();

			// 2.1 Generate the term_info.txt
			CRSMetrics.getInstance().writeTerm_infor(aliases, mTerm_Info, downloadMap, categoryMap);
			// 2.2 Generate the termid_list.txt
			CRSMetrics.getInstance().writeBriefTermid_List(aliases, mTermid_List_tmp);
			// 2.3 Generate the co_mat.txt
			CRSMetrics.getInstance().calculateCoMatrix(mTerm_Info, mTermid_List_tmp, mCo_Matrix_tmp, "2");
			// 2.4 Generate the waf_mat.txt
			CRSMetrics.getInstance().calculateWafMatrix(mCo_Matrix_tmp, mTerm_Info, mWaf_Matrix_tmp, "1e-6");
			// 2.5 Calculate the WafValues
			Map<Integer, Float> returnValue = CRSMetrics.getInstance().calculateWafValues(mWaf_Matrix_tmp, downloadMap,
					totalDownloads, categoryMap, categoryFactorMatrix, scale_size, 5);
			// 2.6 Re-initial Aliases
			List<Aliases> newAliases = CRSMetrics.getInstance().ReinitialAliases(aliases, returnValue);
			new File(mTermid_List_tmp).delete();
			new File(mCo_Matrix_tmp).delete();
			new File(mWaf_Matrix_tmp).delete();

			// 3.1 Generate mAliases.
			CRSMetrics.getInstance().writeAliases(newAliases, mAliases);
			// 3.2 Re-generate mTermid_List
			// 景怡同学的代码.
			CRSMetrics.getInstance().writeGTermid_List(mGScript);
			// 3.3 Re-generate the co_mat.txt
			CRSMetrics.getInstance().calculateCoMatrix(mTerm_Info, mTermid_List, mCo_Matrix, "2");
			// 3.4 Re-generate the waf_mat.txt
			CRSMetrics.getInstance().calculateWafMatrix(mCo_Matrix, mTerm_Info, mWaf_Matrix, "1e-6");
			// 3.5 Re-calculate the WafValues
			Map<Integer, Float> returnValue2 = CRSMetrics.getInstance().calculateWafValues(mWaf_Matrix, downloadMap,
					totalDownloads, categoryMap, categoryFactorMatrix, scale_size, 5);
			// 3.6 Refresh returnValue2
			Iterator iter = returnValue2.entrySet().iterator();
			while (iter.hasNext()) {
				Map.Entry entry = (Map.Entry) iter.next();
				if ((float) entry.getValue() == -1) {
					returnValue2.put((int) entry.getKey(), 0.0f);
				}
			}
			// 4.Generate the Aliases.gexf
			AliasesGenerator.getInstant().addAliase(AliasesFilePath, aliases, returnValue2);
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				reader.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

	// 直接生成简单路径, CRSPR时考虑了下载量和类别.
	@SuppressWarnings("rawtypes")
	public static void main3(String argv[]) {
		Reader reader = null;
		try {
			// 1.Get aliases from the database.
			reader = Resources.getResourceAsReader("Configuration.xml");
			SqlSessionFactoryBuilder ssfBuilder = new SqlSessionFactoryBuilder();
			SqlSessionFactory sqlSessionFactory = ssfBuilder.build(reader);
			SqlSession sqlSession = sqlSessionFactory.openSession();
			AliasesDAO aliasesDAO = sqlSession.getMapper(AliasesDAO.class);
			List<Aliases> aliases = aliasesDAO.findAliases(0);
			TotalDownloadsDAO totalDownloadsDAO = sqlSession.getMapper(TotalDownloadsDAO.class);
			TotalDownloads totalDownloads = totalDownloadsDAO.findTotalDownloads(0);
			HashMap<Integer, Integer> downloadMap = new HashMap<Integer, Integer>();
			HashMap<Integer, Integer> categoryMap = new HashMap<Integer, Integer>();
			FactorUtils categoryFactorMatrix = FactorUtils.getInstance();

			// 2.1 Generate the term_info.txt
			CRSMetrics.getInstance().writeTerm_infor(aliases, mTerm_Info, downloadMap, categoryMap);
			// 2.2 Generate the termid_list.txt
			CRSMetrics.getInstance().writeBriefTermid_List(aliases, mTermid_List);
			// 2.3 Generate the co_mat.txt
			CRSMetrics.getInstance().calculateCoMatrix(mTerm_Info, mTermid_List, mCo_Matrix, "2");
			// 2.4 Generate the waf_mat.txt
			CRSMetrics.getInstance().calculateWafMatrix(mCo_Matrix, mTerm_Info, mWaf_Matrix, "1e-6");
			// 2.5 Calculate the WafValues
			Map<Integer, Float> returnValue = CRSMetrics.getInstance().calculateWafValues(mWaf_Matrix, downloadMap,
					totalDownloads, categoryMap, categoryFactorMatrix, scale_size, 5);

			// 2.6 Refresh returnValue
			Iterator iter = returnValue.entrySet().iterator();
			while (iter.hasNext()) {
				Map.Entry entry = (Map.Entry) iter.next();
				if ((float) entry.getValue() == -1) {
					returnValue.put((int) entry.getKey(), 0.0f);
				}
			}
			// 3.Generate the Aliases.gexf
			AliasesGenerator.getInstant().addAliase(AliasesFilePath, aliases, returnValue);
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				reader.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

	// 直接生成简单路径，CRSPR时未考虑下载量和类别.
	@SuppressWarnings({ "unused" })
	public static void main4(String argv[]) {
		Reader reader = null;
		try {
			// 1.Get aliases from the database.
			reader = Resources.getResourceAsReader("Configuration.xml");
			SqlSessionFactoryBuilder ssfBuilder = new SqlSessionFactoryBuilder();
			SqlSessionFactory sqlSessionFactory = ssfBuilder.build(reader);
			SqlSession sqlSession = sqlSessionFactory.openSession();
			AliasesDAO aliasesDAO = sqlSession.getMapper(AliasesDAO.class);
			List<Aliases> aliases = aliasesDAO.findAliases(0);
			TotalDownloadsDAO totalDownloadsDAO = sqlSession.getMapper(TotalDownloadsDAO.class);
			TotalDownloads totalDownloads = totalDownloadsDAO.findTotalDownloads(0);
			HashMap<Integer, Integer> downloadMap = new HashMap<Integer, Integer>();
			HashMap<Integer, Integer> categoryMap = new HashMap<Integer, Integer>();
			FactorUtils categoryFactorMatrix = FactorUtils.getInstance();

			// 2.1 Generate mAliases.
			CRSMetrics.getInstance().writeAliases(aliases, mAliases);
			// 2.2 Generate the term_info.txt
			CRSMetrics.getInstance().writeTerm_infor(aliases, mTerm_Info, downloadMap, categoryMap);
			// 2.3 Generate the termid_list.txt
			CRSMetrics.getInstance().writeBriefTermid_List(aliases, mTermid_List);
			// 2.4 Generate the co_mat.txt
			CRSMetrics.getInstance().calculateCoMatrix(mTerm_Info, mTermid_List, mCo_Matrix, "2");
			// 2.5 Generate the waf_mat.txt
			CRSMetrics.getInstance().calculateWafMatrix(mCo_Matrix, mTerm_Info, mWaf_Matrix, "1e-6");

			// 3.Calculate the CRSPG-Values
			Map<Integer, Float> returnValue = GEXFGenerator.calculateCRSPG(mAliases, mWaf_Matrix, mCRSPR, null);

			// 4.Generate the Aliases.gexf
			AliasesGenerator.getInstant().addAliase(AliasesFilePath, aliases, returnValue);
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				reader.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

	public static Map<Integer, Float> calculateCRSPG(String mAliases, String mWaf_Matrix, String mCRSPR, Object obj) {
		GEXFGenerator.calculateCRSPG(mAliases, mWaf_Matrix, mCRSPR);
		Map<Integer, Float> returnMap = new HashMap<Integer, Float>();
		BufferedReader reader = null;
		try {
			reader = new BufferedReader(new InputStreamReader(new FileInputStream(mCRSPR)));
			String line;
			while ((line = reader.readLine()) != null) {
				line = line.trim();
				int key = Integer.parseInt(line.split(":")[0].trim());
				float value = Float.parseFloat(line.substring(line.indexOf(":") + 1).trim());
				returnMap.put(key, value);
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		return returnMap;
	}

	// 根据adslist筛选.
	public static void main_filtered_by_exported_and_adslist(String argv[]) {
		Reader reader = null;
		try {
			// 1.Get aliases from the database.
			reader = Resources.getResourceAsReader("Configuration.xml");
			SqlSessionFactoryBuilder ssfBuilder = new SqlSessionFactoryBuilder();
			SqlSessionFactory sqlSessionFactory = ssfBuilder.build(reader);
			SqlSession sqlSession = sqlSessionFactory.openSession();
			AliasesDAO aliasesDAO = sqlSession.getMapper(AliasesDAO.class);
			List<Aliases> aliases = aliasesDAO.findAliasesFilteredByExported(0);

			Map<String, Integer> ThirdPartyMap = LibsCounter.getClusteredClassCount(10, 3);

			// 2.Generate the Aliases.gexf
			AliasesGenerator.getInstant().addAliaseByThirdPartyList(AliasesFilePath, aliases, ThirdPartyMap);
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				reader.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
}
